// i.r(t), i.d(t, { Analytics: () => AnalyticsModule, default: () => o })
import AnalyticsConfig from "../utils/AnalyticsConfig"
import Module from "./Module"
import funcUtil from "../utils/func.util"
export default class AnalyticsModule extends Module {
  endpoints: any[]
  loaded: boolean
  bufferedData: any[]
  constructor() {
    super(...arguments)
    this.name = "analytics"
    this.endpoints = []
    this.loaded = !1
    this.bufferedData = []
  }
  track(e, t) {
    return this.loaded
      ? funcUtil.waitRun(0).then(() => {
          this.endpoints.forEach(i => {
            i.track(e, t)
          })
        })
      : (this.bufferedData.push({ event: e, data: t }), Promise.resolve())
  }
  trackAsync(e, t) {
    return this.loaded
      ? this.endpoints.forEach(i => {
          i.trackAsync(e, t)
        })
      : this.bufferedData.push({ event: e, data: t })
  }
  async load(e, t, i, s, r) {
    this.loaded = !1
    for (const o of e) {
      const { provider: e, token: a, url: l } = o
      if (!AnalyticsConfig[e]) continue
      const c = new AnalyticsConfig[e].class()
      await c.init(i, s)
      r && r.loggedIn && c.identify(r)
      this.endpoints.push(c)
      this.loaded = !0
    }
    return this.loaded && this.flushBufferedData(), this.loaded
  }
  flushBufferedData() {
    for (const e of this.bufferedData)
      this.endpoints.forEach(t => {
        t.track(e.event, e.data)
      })
    this.bufferedData.length = 0
  }
}
// const o = AnalyticsModule
// export default {
//   Analytics: AnalyticsModule,
//   default: o
// }
